package com.example.gw_back_end.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.gw_back_end.domain.PageParam;
import com.example.gw_back_end.domain.PageResult;
import com.example.gw_back_end.domain.entities.GameUserReview;
import com.example.gw_back_end.domain.entities.User;
import com.example.gw_back_end.mapper.GameUserReviewMapper;
import com.example.gw_back_end.mapper.UserMapper;
import com.example.gw_back_end.service.GameUserReviewService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
public class GameUserReviewServiceImpl extends ServiceImpl<GameUserReviewMapper, GameUserReview> implements GameUserReviewService {

    private final UserMapper userMapper;
    private final GameUserReviewMapper gameUserReviewMapper;
    public GameUserReviewServiceImpl(UserMapper userMapper,GameUserReviewMapper gameUserReviewMapper) {
        this.userMapper = userMapper;
        this.gameUserReviewMapper = gameUserReviewMapper;
    }

    // 获取游戏评论列表（根据游戏id）
    @Override
    public PageResult<GameUserReview> getGameUserReviewListByGameId(PageParam pageParam, Integer gameId) {
        // 1. 先查询游戏评论分页数据
        LambdaQueryWrapper<GameUserReview> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(GameUserReview::getGameId, gameId);
        // 按创建时间倒序排列
        lambdaQueryWrapper.orderByDesc(GameUserReview::getCreatedTime);

        Page<GameUserReview> gameUserReviewPage = new Page<>(pageParam.getPage(), pageParam.getSize());
        gameUserReviewPage = gameUserReviewMapper.selectPage(gameUserReviewPage, lambdaQueryWrapper);

        // 2. 如果没有评论数据，直接返回
        if (CollectionUtils.isEmpty(gameUserReviewPage.getRecords())) {
            return PageResult.setPageResult(gameUserReviewPage);
        }

        // 3. 收集所有userId
        Set<Integer> userIds = gameUserReviewPage.getRecords().stream()
                .map(GameUserReview::getUserId)
                .collect(Collectors.toSet());

        // 4. 批量查询用户信息
        Map<Integer, User> userMap = userMapper.selectBatchIds(userIds).stream()
                .collect(Collectors.toMap(User::getId, Function.identity()));

        // 5. 合并用户信息到评论中
        gameUserReviewPage.getRecords().forEach(review -> {
            User user = userMap.get(review.getUserId());
            if (user != null) {
                review.setUsername(user.getUsername());
                review.setAvatar(user.getAvatar());
            }
        });

        // 6. 返回结果
        return PageResult.setPageResult(gameUserReviewPage);
    }

    // 获取游戏评论列表（根据用户id）
    @Override
    public PageResult<GameUserReview> getGameUserReviewListByUserId(PageParam pageParam, Integer userId) {
        LambdaQueryWrapper<GameUserReview> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(GameUserReview::getUserId, userId);
        Page<GameUserReview> gameUserReviewPage = new Page<>(pageParam.getPage(), pageParam.getSize());
        gameUserReviewPage = gameUserReviewMapper.selectPage(gameUserReviewPage, lambdaQueryWrapper);
        return PageResult.setPageResult(gameUserReviewPage);
    }

    // 新增游戏评论
    @Override
    public boolean addGameUserReview(GameUserReview gameUserReview) {
        gameUserReview.setCreatedTime(LocalDateTime.now());
        return gameUserReviewMapper.insert(gameUserReview) > 0;
    }

    // 删除游戏评论
    @Override
    public boolean deleteGameUserReview(Integer id) {
        return gameUserReviewMapper.deleteById(id) > 0;
    }

    // 获取游戏评论
    @Override
    public GameUserReview getGameUserReviewById(Integer id) {
        return gameUserReviewMapper.selectById(id);
    }

    // 更新游戏评论
    @Override
    public boolean updateGameUserReview(GameUserReview gameUserReview) {
        return gameUserReviewMapper.updateById(gameUserReview) > 0;
    }
}
